<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getEmployees">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="姓名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="age" label="年龄" width="80" sortable>
			</el-table-column>
      <el-table-column prop="sex" label="性别" width="80" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.sex == 0">女</span>
          <span v-if="scope.row.sex == 1">男</span>
          <span v-else>未知</span>
        </template>
			</el-table-column>
      <el-table-column prop="state" label="性别" width="80" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state == 0" style="color: red">待激活</span>
          <span v-if="scope.row.state == 1" style="color: greenyellow">正常</span>
          <span v-else>未知</span>
        </template>
			</el-table-column>
      <el-table-column prop="email" label="邮箱" width="180" sortable>
      </el-table-column>
      <el-table-column prop="department.name" label="部门" width="120" sortable>
      </el-table-column>
      <el-table-column prop="shop.name" label="店铺" width="120" sortable>
      </el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination 
          layout="prev, pager, next" 
          :current-page="query.page"
          @current-change="handleCurrentChange" 
          :page-size="query.pageSize" 
          :total="query.total" 
          style="float:right;">
			</el-pagination>
		</el-col>
		<!--新增/修改界面-->
		<el-dialog title="新增/修改" :visible.sync="employeeFormVisible" :close-on-click-modal="false">
			<el-form :model="addEmployee" label-width="80px" :rules="employeeFormRules" ref="addEmployee">
				<el-form-item label="姓名" prop="username">
					<el-input v-model="addEmployee.username" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="addEmployee.password" auto-complete="off"></el-input>
        </el-form-item>
				<el-form-item label="年龄">
					<el-input-number v-model="addEmployee.age" :min="0" :max="200"></el-input-number>
				</el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addEmployee.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="headImage">
          <el-input v-model="addEmployee.headImage" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门">
          <el-select v-model="addEmployee.department" clearable value-key="id" placeholder="请选择">
            <!--下拉选项
              :key=""  //唯一标识
              :label 选择之后展示到选择框中的值
              :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
                 必须要写  value-key="id"
            -->
            <el-option
                v-for="item in department"
                :key="item.id"
                :label="item.name"
                :value="item">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>


			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="employeeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
			query:{
				total: 0,
				page: 1,
        keyword: '',
        pageSize:10

      },
        department:[],
				employees: [],
				listLoading: false,
				sels: [],//列表选中列

				
				employeeFormVisible: false,//新增界面是否显示
				addLoading: false,
				employeeFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增/修改界面数据
				addEmployee: {
				  id:null,
          username:null,
          password:'',
          email:'',
          headImage:'',
          age:0,
          department:{
            id:null,
            name:''
          }
				}

			}
		},
		methods: {
		  //获取部门列表
      getDepartment(){
        this.$http.get("/department/list")
                  .then(result =>{
                    result = result.data;
                    if(result.success){
                      this.department = result.result
                      console.log(this.department)
                    }
                  })
      },

      //获取当前行数据
      handleCurrentChange(val) {
				this.query.page = val;
				this.getEmployees();
			},
			//获取用户列表
			getEmployees() {
				this.listLoading = true;
				//NProgress.start();
			this.$http.post("/employee",this.query)
                .then((res) => {
                  res = res.data;
                  console.log(res);
                  if(res.success){

                    this.employees = res.result.datas;
                    this.query.total = res.result.total;
                  }
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
          // debugger
          this.$http.delete("/employee/"+row.id)
                    .then((res) => {
                      // console.log("id:"+row.id)
						        this.listLoading = false;
                      if(res.data.success){
						            this.$message({ message: '删除成功', type: 'success' });
                      //删除以后重新从第一页开始查询
						        this.getEmployees();
                      }else{
                        this.$message({ message: '删除失败'+res.data.message, type: 'error' });
                      }
					});
				}).catch(() => {
          this.$message({ message: '已取消删除', type: 'info' });
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.employeeFormVisible = true;
        this.getDepartment();
				this.addEmployee = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.employeeFormVisible = true;
				this.addEmployee = {
          id:null,
          username:null,
          password:'',
          email:'',
          headImage:'',
          age:0,
          department:{
            id:null,
            name:'',
          }
				};
				this.getDepartment();
			},
			
			//新增
			save: function () {
        debugger
				this.$refs.addEmployee.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addEmployee);

							console.log("id:"+para.id)
						this.$http.put("/employee",para)
                      .then((res) => {
								      this.addLoading = false;
                        if(res.data.success){
                          this.$message({ message: '保存成功', type: 'success' });
                          this.employeeFormVisible = false;
                          this.query.page = 1;
                          this.getEmployees();
                        }else{
                          this.$message({ message: '保存失败'+res.data.message, type: 'success' });
                        }
							});
						}).catch(() => {
              this.$message({ message: '已取消新增操作', type: 'info' });
            });
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					this.$http.patch("/employee",ids)
                    .then((res) => {
						        this.listLoading = false;
						        if(res.data.success){
                    this.$message({ message: '删除成功', type: 'success' });

                    }else{
                    this.$message({ message: '删除失败'+res.data.message, type: 'error' });

                    }
                    this.query.page = 1;
                    this.getEmployees();
					});
				}).catch(() => {
          this.$message({ message: '已取消批量删除', type: 'info' });
				});
			}
		},
		mounted() {
			this.getEmployees();
		}
	}

</script>

<style scoped>

</style>